/**
 * createStore(reducer, preloadedState, enhancer)
 * {
 *  getState, dispatch, subscribe
 * }
 */

 function createStore(reducer, preloadedState) {
     // store 对象中存储的状态
    var currentState = preloadedState;
    // 存放订阅者函数
    var currentListeners = []
    // 获取状态
    function getState() {
        return currentState;
    }
    //触发action
    function disPatch(action) {
        currentState = reducer(currentState, action)
        //状态改变 循环订阅者数组
        currentListeners.forEach(listener => {
            // 获取 并调用订阅者
            listener()
        })
    }
    //订阅状态 可以被多次订阅
    function subscribe (listener) {
        currentListeners.push(listener)
    }

    return {
        getState,
        disPatch,
        subscribe
    }
 }